<!-- 用户编辑弹窗 -->
<template>
  <el-dialog width="680px" :visible="visible" :lock-scroll="false" :destroy-on-close="true"
    custom-class="ele-dialog-form" title="绑定班级" @update:visible="updateVisible">
    <el-form ref="form" :model="form" :rules="rules" label-width="90px">
      <el-row :gutter="15">
        <el-col :sm="12">
          <el-form-item label="所属学校:" prop="schoole_id">
            <el-select style="width:100%" clearable v-model="form.schoole_id" placeholder="请输入所属学校">
              <el-option :value="item.id" :label="item.title" v-for="(item,index) in schoolList" :key="index" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :sm="12">
          <el-form-item label="年级:" prop="grade_id">
            <el-select style="width:100%" clearable v-model="form.grade_id" placeholder="请输入年级">
              <el-option :value="item.id" :label="item.title" v-for="(item,index) in gradeList" :key="index" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :sm="12">
          <el-form-item label="班级:" prop="class_id">
            <el-select style="width:100%" clearable v-model="form.class_id" placeholder="请输入班级">
              <el-option :value="item.id" :label="item.title" v-for="(item,index) in classList" :key="index" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer">
      <el-button @click="updateVisible(false)">取消
      </el-button>
      <el-button type="primary" :loading="loading" @click="save">确定
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
  import uploadImage from '@/components/uploadImage';

  export default {
    name: 'UserEdit',
    props: {
      // 弹窗是否打开
      visible: Boolean,
      // 修改回显的数据
      data: Object,
    },
    components: {
      uploadImage
    },
    data() {
      return {
        // 表单数据
        form: Object.assign({}, this.data),
        // 表单验证规则
        rules: {
          schoole_id: [{
            required: true,
            message: '请输入学校',
            trigger: 'blur'
          }],
        },
        // 提交状态
        loading: false,
        // 是否是修改
        schoolList: [],
        gradeList: [],
        classList: [],
      };
    },
    watch: {
      data() {
        if (this.data) {
          this.form = this.data;
        } else {
          this.form = {};
        }
      },
      'form.schoole_id': {
        handler: function() {
          this.getGradeList()
        },
        deep: true
      },
      'form.grade_id': {
        handler: function() {
          this.getClassList()
        },
        deep: true
      }
    },
    mounted() {
      this.getSchoolList()
    },
    methods: {
      getSchoolList() {
        this.$http.get("/schoole/index", {
            params: {
              page: 1,
              limit: 999,
            }
          }).then(res => {
            this.schoolList = res.data.data;
          })
          .catch(e => {
            this.$message.error(e.message);
          });
      },
      getGradeList() {
        this.$http
          .get("/student/getGrade", {
            params: {
              schoole_id: this.form.schoole_id
            }
          })
          .then(res => {
            this.gradeList = res.data.data;
          })
          .catch(e => {
            this.$message.error(e.message);
          });
      },
      getClassList() {
        this.$http
          .get("/student/getClass", {
            params: {
              grade_id: this.form.grade_id,
              schoole_id: this.form.schoole_id
            }
          })
          .then(res => {
            this.classList = res.data.data;
          })
          .catch(e => {
            this.$message.error(e.message);
          });
      },
      /* 保存编辑 */
      save() {
        this.$refs['form'].validate((valid) => {
          if (valid) {
            this.loading = true;
            this.$http.post('/device_bind/stuedit', {
              id: this.form.id,
              schoole_id: this.form.schoole_id,
              grade_id: this.form.grade_id,
              class_id: this.form.class_id

            }).then(res => {
              this.loading = false;
              if (res.data.code === 0) {
                this.$message({
                  type: 'success',
                  message: res.data.msg
                });
                this.updateVisible(false);
                this.$emit('done');
              } else {
                this.$message.error(res.data.msg);
              }
            }).catch(e => {
              this.loading = false;
              this.$message.error(e.message);
            });
          } else {
            return false;
          }
        });
      },
      /* 更新visible */
      updateVisible(value) {
        this.$emit('update:visible', value);
        if (!value) {
          this.$emit('done');
        }
      },


    }
  }
</script>

<style scoped>
</style>
